/**
 * Android FriendFeed Client
 * Copyright (C) 2010 E & B Labs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.eblabs.androidfriendfeedclient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.eblabs.friendfeed.ApiException;
import net.eblabs.friendfeed.FriendFeedAPI;

import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author epplestun
 */
public class FriendfeedAddCommentActivity  extends Activity {
    @Override
    public final void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.addcomment);

        final Preferences preferences = Preferences.getInstance(this);

        setTitle(R.string.send_comment);

        Intent getIntent = getIntent();
        Bundle entry = getIntent.getBundleExtra("entry");

        ImageButton sendCommentButton = (ImageButton)
                findViewById(R.id.sendComment);
        final EditText text = (EditText) findViewById(R.id.comment);
        final TextView entryText = (TextView) findViewById(R.id.entry);
        entryText.setText(entry.getString("body"));
        final Activity activity = this;

        sendCommentButton.setOnClickListener(new ImageButton.OnClickListener() {
            public void onClick(View v) {
                Intent getIntent = getIntent();
                Bundle entry = getIntent.getBundleExtra("entry");
                FriendFeedAPI api = FriendFeedAPI.getInstance(
                        preferences.getUsername(), preferences.getRemoteKey());
                try {
                    JSONObject result = api.addComment(
                            text.getText().toString(), entry.getString("id"));
                    Toast.makeText(activity, R.string.send_comment_ok,
                            Toast.LENGTH_LONG).show();
                } catch (ApiException ex) {
                    Logger.getLogger(FriendfeedAddCommentActivity.class
                            .getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(FriendfeedAddCommentActivity.class
                            .getName()).log(Level.SEVERE, null, ex);
                } catch (JSONException ex) {
                    Logger.getLogger(FriendfeedAddCommentActivity.class
                            .getName()).log(Level.SEVERE, null, ex);
                } catch (URISyntaxException ex) {
                    Logger.getLogger(FriendfeedAddCommentActivity.class
                            .getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    /* {@inheritDoc}. */
    @Override
    protected final void onStop() {
        super.onStop();
    }
}